<template>
  <div class="comment">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="用户id" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="歌曲编号" prop="songId">
        <el-input
          v-model="queryParams.songId"
          placeholder="请输入歌曲编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="歌单编号" prop="songListId">
        <el-input
          v-model="queryParams.songListId"
          placeholder="请输入歌单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户删除">
        <el-select v-model="queryParams.isDeleted" placeholder="请选择" clearable >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="开始时间" prop="createTime">
        <el-date-picker clearable
            v-model="queryParams.createTime"
            type="date"
            value-format="yyyy-MM-dd hh:mm:ss"
            placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="updateTime">
        <el-date-picker clearable
                        v-model="queryParams.updateTime"
                        type="date"
                        value-format="yyyy-MM-dd hh:mm:ss"
                        placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>

<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->

    </el-row>

    <el-table v-loading="loading" :data="commentList" @selection-change="handleSelectionChange">
      <el-table-column :show-overflow-tooltip="true"  type="selection" width="55" align="center"/>
      <el-table-column :show-overflow-tooltip="true"  label="评论编号" align="center" prop="id"/>
      <el-table-column :show-overflow-tooltip="true"  label="评论用户" align="center">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            :title="'用户编号'+scope.row.memberId"
            width="200"
            trigger="hover"
            :content="scope.row.memberNickname">
            <el-image :fit="'fit'" :src="scope.row.memberAvatar"  slot="reference" ></el-image>
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column :show-overflow-tooltip="true"  label="回复用户" align="center" >
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            :title="'用户编号'+scope.row.toCommentId"
            width="200"
            trigger="hover"
            :content="scope.row.toMemberNickname">
            <el-image :size="20" :fit="'fill'" :src="scope.row.toMemberAvatar"  slot="reference" ></el-image>
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column :show-overflow-tooltip="true"  label="歌曲编号" align="center" prop="songId"/>
      <el-table-column :show-overflow-tooltip="true"  label="歌单编号" align="center" prop="songListId"/>
      <el-table-column :show-overflow-tooltip="true"  label="评论内容"  align="center" prop="content" width="180">
        <template slot-scope="scope">
          <el-input
            type="textarea"
            width="180"
            :readonly="true"
            placeholder="请输入内容"
            v-model="scope.row.content">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true"  label="点赞数" align="center" prop="tags"/>
      <el-table-column :show-overflow-tooltip="true"  label="楼层" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.floor == 0">默认</el-tag>
          <el-tag type="info" v-else>{{ scope.row.floor }}楼</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否删除" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isDeleted == 0">用户未删除</el-tag>
          <el-tag type="success" v-else-if="scope.row.isDeleted == 1">管理员删除</el-tag>
          <el-tag type="info" v-else-if="scope.row.isDeleted == 2">用户已删除</el-tag>
        </template>
      </el-table-column>
      <el-table-column  :show-overflow-tooltip="true" label="评论时间" align="center">
        <template slot-scope="scope">
          <el-tag type="info">{{formatCreateTime(scope.row.createTime)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    <pagination-->
<!--      v-show="total>0"-->
<!--      :total="total"-->
<!--      :page.sync="queryParams.pageNum"-->
<!--      :limit.sync="queryParams.pageSize"-->
<!--      @pagination="getList"-->
<!--    >-->
<!--    </pagination>-->

    <!-- 添加或修改评论对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入用户id"/>
        </el-form-item>
        <el-form-item label="回复评论" prop="toCommentId">
          <el-input v-model="form.toCommentId" placeholder="请输入回复评论id"/>
        </el-form-item>
        <el-form-item label="歌曲编号" prop="songId">
          <el-input v-model="form.songId" placeholder="请输入歌曲编号"/>
        </el-form-item>
        <el-form-item label="歌单编号" prop="songListId">
          <el-input v-model="form.songListId" placeholder="请输入歌单编号"/>
        </el-form-item>
        <el-form-item label="评论内容">
          <el-input type="textarea" v-model="form.content"></el-input>
        </el-form-item>
        <el-form-item label="点赞数" prop="tags">
          <el-input v-model="form.tags" placeholder="请输入点赞数"/>
        </el-form-item>
        <el-form-item label="楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入楼层"/>
        </el-form-item>
        <el-form-item label="是否删除" prop="isDeleted">
          <el-input v-model="form.isDeleted" placeholder="请输入是否删除"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryParams.total">
      </el-pagination>
    </div>

  </div>
</template>

<script>

import {formatDate} from "../../api/index";

export default {
  name: "comment",
  data() {
    return {
      currentPage4: 4, //当前分页数
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评论表格数据
      commentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        total: 0,
        pageNum: 1,
        pageSize: 3,
        memberId: null,
        songId: null,
        songListId: null,
        content: null,
        tags: null,
        floor: null,
        isDeleted: null,
        createTime: null,
        updateTime: null,
        toCommentId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 删除选择
      options: [{
        value: '0',
        label: '用户未删除'
      },
        {
          value: '1',
          label: '管理员已删除'
        },
        {
        value: '2',
        label: '用户已删除'
      }],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /*格式化时间 format(new Date()) */
    formatCreateTime(createTime) {
      return formatDate(createTime)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryParams.pageNum = val;
      this.getList();
    },

    /** 查询评论列表 */
    getList() {
      this.loading = true;
      console.log("查询的条件：");
      console.log(this.queryParams)
      this.$axios.post(`/sys/comment/queryPage?pageNum=${this.queryParams.pageNum}&pageSize=${this.queryParams.pageSize}`, this.queryParams).then((res)=> {
          let pageData = res.data.data
          console.log(pageData)
          this.queryParams.pageNum = pageData.pageNum;
          this.queryParams.pageSize = pageData.pageSize;
          this.queryParams.total = Number(pageData.total);
          this.commentList = pageData.list;
          this.loading = false;
        })
        .catch((err)=> {

        })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        memberId: null,
        songId: null,
        songListId: null,
        content: null,
        tags: null,
        floor: null,
        isDeleted: null,
        createTime: null,
        updateTime: null,
        toCommentId: null
      };
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 3;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {}
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 3;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加评论";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      console.log(row);
      const id = row.id || this.ids
      this.$axios.get('/sys/comment/'+id, {
        params: {
          id: id
        }}).then((res)=> {
          console.log(res);
          this.form = res.data.data;
          this.open = true;
          this.title = "修改评论";
        })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            // this.form.createTime = null;
            // this.form.updateTime = null;
            console.log(this.form);
            this.$axios.post('/sys/comment/update',
              this.form
            ).then((res) => {
              this.$message({
                message: '修改成功',
                type: 'success'
              });
              this.open = false;
              this.getList();
            }).catch((err) => {
            })
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      // 批量删除拼接 1,2,3,
      let idStr = "";
      if(row.id) {
        idStr+=row.id+",";
      } else {
        ids.forEach((e)=> {
          idStr+=e+",";
        })
      }

      this.$confirm('确认删除评论 ' + ids + '?')
        .then(res => {
          this.$axios.delete('/sys/comment/batch/' + idStr).then((res)=> {
            console.log(res)
            this.getList();
          }).catch((err)=> {

          })
        })
        .catch(e => {});
    }
  }
};
</script>

<style scoped>

</style>
